<?php
require_once("../../../config.inc.php");
require_once("../../../app/_inc/include.php");
require_once("profileFunction.php");

$errMsg = "";
$description = "";
$note = "";
$description = sxREQUEST("description");
$note = sxREQUEST("note");
$id_profile = sxREQUEST("id_profile");
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "save"){
	$errMsg .= profileMod();	
}
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "delete"){
	$errMsg .= profileDel();	
}
if(isset($_REQUEST["id"])){
	$id_profile = $_REQUEST["id"];
}
$sSQL = "SELECT p.* FROM profile p " .
		" WHERE p.id_profile=$id_profile";
$objSDAO->query($sSQL, "sel");
$row = $objSDAO->fetch("sel");
$description = $row["description"];
$note = $row["note"];
$sSQL = "SELECT * FROM flag_profile WHERE id_profile=$id_profile";
$objSDAO->query($sSQL, "selFlag");
$sSQL = "SELECT u.* FROM user u " .
		"INNER JOIN user_profile up ON u.id_user=up.id_user " .
		"WHERE up.id_profile=$id_profile " .
		"ORDER BY u.last_name, u.name";
$objSDAO->query($sSQL, "selUser");
$sSQL = "SELECT da.*, dap.permission FROM data_area da " .
		" LEFT JOIN data_area_permission dap ON dap.data_area = da.data_area" .
		" AND dap.id_profile=$id_profile";
$objSDAO->query($sSQL, "seldap");
if (!$row){
	$errMsg.= _t("Profilo non definito");
}

//output HTML
$aOpz = array("tit2"=>_t("Visualizza il profilo"));
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/ajax.js"></script> 
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		var errMsg = "";
		var objFrm = document.getElementById("objFrm");
		var objMessArea =  document.getElementById("messArea");
		
		var obj = document.getElementById("description");
		if(obj.value == ""){
			errMsg += "<?php echo _t("La description &egrave; obbligatoria");?>.<br>";
			obj.className = "inpt01";
			//obj.focus();
		}else{
			obj.className = "inpt01";
		}
		if(errMsg == ""){
			objFrm.submit();
		}else{
			objMessArea.innerHTML = errMsg;
		}		
	}
	function showHide(ctrl){
		var obj = document.getElementById(ctrl);
		if(obj.className == "hidden")
			obj.className = "show";
		else
			obj.className = "hidden"
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"profileMod.php\" method=\"POST\">");
echo("<input type=\"hidden\" name=\"xsn\" id=\"xsn\" value=\"".XSN."\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"id_profile\" id=\"id_profile\" value=\"$id_profile\">");
echo("<input class=\"inpt01\" type=\"hidden\" name=\"description\" id=\"description\" maxlength=\"128\" size=\"32\" value=\"".sxHE($description)."\">");
//output comandi
$aCmd = array(
	"usrlis"=>array("pag"=>"../user/userList.php",
						"title"=>_t("lista uytenti"),
						"text"=>_t("utenti"),
						"perm"=>"visuten", "align"=>"L"),
	"flag"=>array("pag"=>"profileFlagIns.php?id=$id_profile",
						"title"=>_t("Modifica flag del profilo"),
						"text"=>_t("modifica flag"),
						"perm"=>"updpro"),
	"user"=>array("pag"=>"profileUserIns.php?id=$id_profile&",
						"title"=>_t("Abilita utenti al profilo"),
						"text"=>_t("associa utenti"),
						"perm"=>"updpro"),
	"datare"=>array("pag"=>"profileDataAreaIns.php?id=$id_profile&",
						"title"=>_t("Abilita aree dati"),
						"text"=>_t("abilita aree dati"),
						"perm"=>"updpro"),
	"del"=>array("pag"=>"javascript:confirmDelete()",
						"title"=>_t("Elimina il profilo"),
						"text"=>_t("elimina"),
						"perm"=>"updpro"),
	"ins"=>array("pag"=>"profileMod.php?id=$id_profile",
						"title"=>_t("Modifica il profilo"),
						"text"=>_t("modifica"),
						"perm"=>"updpro"),
	"back"=>array("pag"=>$objUser->getHistory(-1,false),
						"title"=>$objUser->getHistoryText(-1),
						"text"=>$objUser->getHistoryText(-1))
	);
echo(commandsHTML($aCmd));	
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan=\"2\">"._t("Profilo")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"20%\">"._t("description")."</td>" .
			"<td class=\"text01\">".sxHE($description)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("note")."</td>" .
			"<td class=\"text01\"><pre class=\"text01\">".sxWW(sxHE($note))."</td>" .
		"</tr>\n");
echo("<tr>" .
	"<td class=\"labl01\">"._t("aree dati")."</td>" .
	"<td class=\"text01\">");	
$pos=0;
$sep='';
while($row = $objSDAO->fetch("seldap",$pos)){
	$pos='';
	if ($row["permission"]=='N') continue;
	echo $sep.sxHE($row["data_area"])."(".(($row["permission"]=='M')?'modifica':'visualizza').")";
	$sep = ', ';
}
echo("</td>" .
		"</tr>\n");
echo("</table>");
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
//flag associati al profilo
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\">"._t("Flag profilo")."</td>" .
			"<td class=\"titl01\" width=\"80%\"><a href=\"#\" onClick=\"javascript:showHide('flags')\">".
			"<img border=\"0\" src=\"".SERVER_URL_ROOT."/app/_img/b_search.png\" title=\"Clicca per visualizzare i flag associati al profilo\"></a></td>" .
		"</tr>\n");
echo("</table>\n");
echo("<div id=\"flags\" class=\"show\">");
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
$aFlagprofile = array();
require('../../../app/_inc/aProFlag.php');
foreach($aProFlag as $flag =>$aFlg) {
	foreach($aFlg['val'] as $val => $dv) {
		$aFlagProfile[$flag] = $val;
		break;
	}
}
//print_r($aFlagProfile);
while($row = $objSDAO->fetch("selFlag")){
	$aFlagProfile[$row["flag"]] = $row["val_flag"];
}
echo("<tr>" .
			"<td class=\"tabh01\" width=\"5%\">"._t('flag')."</td>" .
			"<td class=\"tabh01\" width=\"45%\">"._t('description')."</td>" .
			"<td class=\"tabh01\" width=\"5%\">"._t('val.')."</td>" .
			"<td class=\"tabh01\" width=\"45%\">"._t('description')."</td>" .
		"</tr>\n");
foreach($aFlagProfile as $flag => $val) {
	echo("<tr>" .
				"<td class=\"text01\">".$flag."</td>" .
				"<td class=\"text01\">".sxHE($aProFlag[$flag]['des'])."</td>" .
				"<td class=\"text01\">".sxHE($val)."</td>" .
				"<td class=\"text01\">".sxHE($aProFlag[$flag]['val'][$val])."</td>" .
			"</tr>\n");
}
echo("</table>\n");
echo("</div>\n");
//utenti associati al profilo
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\">"._t("Utenti associati al profilo")."</td>" .
			"<td class=\"titl01\" width=\"80%\"><a href=\"#\" onClick=\"javascript:showHide('users')\">".
			"<img border=\"0\" src=\"".SERVER_URL_ROOT."/app/_img/b_search.png\" title=\"Clicca per visualizzare gli utenti associati al profilo\"></a></td>" .
		"</tr>\n");
echo("</table>\n");
echo("<div id=\"users\" class=\"show\">");
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
$num = 0;
while($row = $objSDAO->fetch("selUser")){
	$num ++;
	echo("<tr>" .
				"<td class=\"labl01\" width=\"20%\">".$row["uid"]."</td>" .
				"<td class=\"text01\" width=\"80%\">".sxHE($row["name"])." ".sxHE($row["last_name"])."</td>" .
			"</tr>\n");
}
if($num == 0){
	echo("<tr>" .
				"<td class=\"labl01\" colspan=\"2\">"._t("Nessun utente associato al profilo")."</td>" .
			"</tr>\n");
}
echo("</table>\n");
echo("</div>\n");

echo(commandsHTML($aCmd));
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
$objSDAO->close();
?>
